﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sand_game_core
{
    public class Fire : particle
    {
        public Fire(uint x, uint y, int startLight)
            : base(new Microsoft.Xna.Framework.Color(230 + random.rnd.Next(25), 130 + random.rnd.Next(60), 60 + random.rnd.Next(60)), startLight)
        {
            base.x = x;
            base.y = y;
        }
        public override void update()
        {
            upParticle = world.arrayheight > y + 1 ? world.worldParticleLayer[x * world.arraywidth + y + 1] : null;
                downParticle = y >= 1 ? world.worldParticleLayer[x * world.arraywidth + y - 1] : null;
            ammount = (uint)random.rnd.Next(3);
            if (ammount > 0)
            {
                //if (upParticle == null)
                {
                    world.swap(x, y, x, y + 1);
                }
                    RightParticle = x >= ammount ? world.worldParticleLayer[(x - ammount) * world.arraywidth + y] : null ;
                    LeftParticle = world.arraywidth > y + ammount ? world.worldParticleLayer[(x + ammount) * world.arraywidth + y] : null;
                if (RightParticle == null && !(LeftParticle == null))
                {
                    world.swap(x, y, x - ammount, y);
                }
                else if (LeftParticle == null && !(RightParticle == null))
                {
                    world.swap(x, y, x + ammount, y);
                }
                else
                {
                    if (random.rnd.Next(2) == 0)
                    {
                        world.swap(x, y, x + ammount, y);
                    }
                    else
                    {
                        world.swap(x, y, x - ammount, y);
                    }
                }
            }
                toTransition = random.rnd.Next(25) == 0 ? new Empty(x, y, light) : (upParticle is Water ? new Empty(x, y, light) :null);
        }

        public override void lightMe()
        {
                light += light < 200 ? 1 : 0;
        }

        public override void reactivate()
        {
            active = (x + 1 < world.arraywidth && y + 1 < world.arrayheight && x - 1 < world.arraywidth && y - 1 < world.arrayheight && (world.worldParticleLayer[(x + 1) * world.arraywidth + y] == null || world.worldParticleLayer[(x - 1) * world.arraywidth + y] == null || world.worldParticleLayer[x * world.arraywidth + y + 1] == null || world.worldParticleLayer[x * world.arraywidth + y - 1] == null)) ? true : false;
        }
    }
}